package Servlet.Test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import CodeAdmin.Code;
import Creche.Bd.BD;
import Creche.Main.Creche;
import Extraction.Extraction;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@SuppressWarnings("serial")
public class LoadDB extends HttpServlet {
	
	private ArrayList<Creche> creches;

	
	/**
	 * Retourne sous forme de string json les donn�es contenue dans la base de donn�es pour une creche.
	 * @param request est un HttpServletRequest
	 * @param response est un HttpServletResponse
	 */

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {

		response.setContentType("text/html");
		PrintWriter page = response.getWriter();
		

		BD myBD = new BD();
		System.out.println("Suppression de tout les donn�es");
		myBD.removeAll();
		
		
		System.out.println("Chargement des creches");
		Extraction e = new Extraction();
		myBD.estPersistents(e.getListCreches());
		creches = myBD.getCreches();

		
		Code code = new Code(true);
		code.setCode("code10admin");
		myBD.estPersistent(code);
			
		
		// On utilise JSON
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String jsonResult = gson.toJson(creches);
		
		// On renvois les donn�es
		response.setContentType("application/json");
		page.print(jsonResult);
		
		
	}
	
}